//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.sys;

import com.aidong.dao.base.OrderSet;
import com.aidong.dao.wills.SysMenuDao;
import com.aidong.model.MenuAuthority;
import com.aidong.model.wills.SysMenu;
import com.aidong.web.controller.BaseController;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"sysMenu"})
public class SysMenuController extends BaseController {
    @Resource
    SysMenuDao sysMenuDao;

    public SysMenuController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return new ModelAndView("sys/menu/list");
    }

    @RequestMapping({"_list"})
    @ResponseBody
    public String _list() {
        List list = this.sysMenuDao.queryList((SysMenu)null, (new OrderSet()).asc("parentId").asc("sort"));
        StringBuilder sb = new StringBuilder();
        sb.append("[");

        SysMenu menu;
        for(Iterator i$ = list.iterator(); i$.hasNext(); sb.append("{id:\"").append(menu.getMenuId()).append("\"").append(",name:\"").append(menu.getMenuName()).append("\"").append(",pid:\"").append(menu.getParentId()).append("\"").append("}")) {
            menu = (SysMenu)i$.next();
            if(sb.length() > 10) {
                sb.append(", ");
            }
        }

        sb.append("]");
        return this.getResult(true, sb.toString(), "");
    }

    @RequestMapping({"add"})
    public ModelAndView add(@RequestParam("pid") int pid) {
        SysMenu menu0 = (SysMenu)this.sysMenuDao.getById(Integer.valueOf(pid));
        if(menu0 == null) {
            return this.showAlert("上级菜单错误");
        } else {
            MenuAuthority menu = new MenuAuthority();
            menu.setParentId(Integer.valueOf(pid));
            menu.setMenuShow(Integer.valueOf(1));
            menu.setShortcut(Integer.valueOf(0));
            ModelAndView mv = new ModelAndView("sys/menu/edit");
            mv.addObject("menu", menu);
            mv.addObject("pname", menu0.getMenuName());
            mv.addObject("_oper", "add");
            return mv;
        }
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("menu") SysMenu menu, @RequestParam(value = "operates",required = false) int[] operates, @RequestParam(value = "menuShows",required = false) int[] menuShows) {
        if(this.chkNull(menu.getMenuName())) {
            return this.getResult(false, "请输入名称");
        } else if(this.chkNullZ(menu.getSort())) {
            return this.getResult(false, "请输入排序号");
        } else {
            int op = 0;
            int len$;
            int i$;
            if(operates != null && operates.length > 0) {
                int[] ms = operates;
                int e = operates.length;

                for(len$ = 0; len$ < e; ++len$) {
                    i$ = ms[len$];
                    op += i$;
                }
            }

            menu.setOperate(Integer.valueOf(op));
            int var11 = 0;
            if(menuShows != null && menuShows.length > 0) {
                int[] var12 = menuShows;
                len$ = menuShows.length;

                for(i$ = 0; i$ < len$; ++i$) {
                    int m = var12[i$];
                    var11 += m;
                }
            }

            menu.setMenuShow(Integer.valueOf(var11));
            menu.setUpdateTime(new Date());

            try {
                this.sysMenuDao.add(menu);
                this.addUserLog("新增系统菜单", "编号:" + menu.getMenuId() + ",名称:" + menu.getMenuName());
            } catch (Exception var10) {
                this.log.error("add sysmenu", var10);
                return this.getResult(false, "保存发生错误");
            }

            return this.getResult(true, "保存完成");
        }
    }

    @RequestMapping({"edit"})
    public ModelAndView edit(@RequestParam("menuId") int menuId) {
        MenuAuthority menu = this.sysMenuDao.getMenuAuthorityById(menuId);
        if(menu == null) {
            return this.showAlert("没有这个菜单");
        } else {
            SysMenu menu0 = (SysMenu)this.sysMenuDao.getById(menu.getParentId());
            ModelAndView mv = new ModelAndView("sys/menu/edit");
            mv.addObject("menu", menu);
            if(menu0 != null) {
                mv.addObject("pname", menu0.getMenuName());
            }

            mv.addObject("_oper", "edit");
            return mv;
        }
    }

    @RequestMapping({"_edit"})
    @ResponseBody
    public String _edit(@ModelAttribute("menu") SysMenu menu, @RequestParam(value = "operates",required = false) int[] operates, @RequestParam(value = "menuShows",required = false) int[] menuShows) {
        if(this.chkNull(menu.getMenuName())) {
            return this.getResult(false, "请输入名称");
        } else if(this.chkNullZ(menu.getSort())) {
            return this.getResult(false, "请输入排序号");
        } else {
            if(this.chkNullZ(menu.getShortcut())) {
                menu.setShortcut(Integer.valueOf(0));
            }

            SysMenu menu1 = (SysMenu)this.sysMenuDao.getById(menu.getMenuId());
            if(menu1 == null) {
                return this.getResult(false, "没有这个菜单");
            } else {
                menu1.setMenuName(menu.getMenuName());
                menu1.setLinkUrl(menu.getLinkUrl());
                menu1.setCodeName(menu.getCodeName());
                menu1.setOperateEx(menu.getOperateEx());
                menu1.setMenuShow(menu.getMenuShow());
                menu1.setSort(menu.getSort());
                menu1.setShortcut(menu.getShortcut());
                int op = 0;
                int len$;
                int i$;
                if(operates != null && operates.length > 0) {
                    int[] ms = operates;
                    int e = operates.length;

                    for(len$ = 0; len$ < e; ++len$) {
                        i$ = ms[len$];
                        op += i$;
                    }
                }

                menu1.setOperate(Integer.valueOf(op));
                int var12 = 0;
                if(menuShows != null && menuShows.length > 0) {
                    int[] var13 = menuShows;
                    len$ = menuShows.length;

                    for(i$ = 0; i$ < len$; ++i$) {
                        int m = var13[i$];
                        var12 += m;
                    }
                }

                menu1.setMenuShow(Integer.valueOf(var12));
                menu1.setUpdateTime(new Date());

                try {
                    this.sysMenuDao.update(menu1);
                    this.addUserLog("修改系统菜单", "编号:" + menu.getMenuId() + ",名称:" + menu.getMenuName());
                } catch (Exception var11) {
                    this.log.error("edit sysmenu", var11);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        }
    }

    @RequestMapping({"_del"})
    @ResponseBody
    public String _del(@RequestParam("menuId") int menuId) {
        SysMenu menu = (SysMenu)this.sysMenuDao.getById(Integer.valueOf(menuId));
        if(menu == null) {
            return this.getResult(false, "没有这个菜单");
        } else {
            try {
                this.sysMenuDao.delete(menu);
                this.addUserLog("删除系统菜单", "编号:" + menu.getMenuId() + ",名称:" + menu.getMenuName());
            } catch (Exception var4) {
                this.log.error("del sysmenu", var4);
                return this.getResult(false, "保存发生错误");
            }

            return this.getResult(true, "删除成功");
        }
    }
}
